package 打牢基础30天.day02.my;

import 打牢基础30天.util.ArrUtil;

/**
 * @Author yaya
 * @Create 2024/6/4 20:04
 * Description: 归并排序，递归
 */
public class Code01_MergeSort {
    public static void mergeSort(int[] arr) {
        if (arr == null || arr.length < 2) return;
            mergeSort(arr, 0, arr.length - 1);
    }

    public static void mergeSort(int[] arr, int left, int right) {
        if (left == right) return;
        int mid = left + ((right - left) >> 1);
        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);
        merge(arr, mid, left, right);
    }

    private static void merge(int[] arr, int mid, int left, int right) {
        int p1 = left;
        int p2 = mid + 1;
        int i = 0;
        int[] help = new int[right - left + 1];

        while (p1 <= mid && p2 <= right) {
            // 谁小放谁
            help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <= mid) {
            help[i++] = arr[p1++];
        }
        while (p2 <= right){
            help[i++] = arr[p2++];
        }
        for (i = 0; i < help.length; i++) {
            arr[left + i] = help[i];
        }
    }

    public static void mergeSort2(int[] arr){
        if (arr == null || arr.length < 2){
            return;
        }
        mergeSort2(arr,0, arr.length - 1);
    }

    private static void mergeSort2(int[] arr, int left, int right) {
        if (left == right) return;

        int mid = left + ((right - left) >> 1);
        mergeSort2(arr,left, mid);
        mergeSort2(arr,mid + 1, right);
        merge2(arr,mid,left,right);
    }

    private static void merge2(int[] arr, int mid, int left, int right) {
        int p1 = left;
        int p2 = mid + 1;
        int[] help = new int[right - left + 1];
        int i = 0;
        while (p1 <= mid && p2 <= right){
            help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++];
        }
        while (p1 <= mid){
            help[i++] = arr[p1++];
        }
        while (p2 <= right){
            help[i++] = arr[p2++];
        }

        for (i = 0; i < help.length; i++) {
            arr[left + i] = help[i];
        }
    }

    // test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = ArrUtil.generateRandomArray(maxSize, maxValue);
            int[] arr2 = ArrUtil.copyArray(arr1);
            mergeSort2(arr1);
            ArrUtil.comparator(arr2);
            if (!ArrUtil.isEqual(arr1, arr2)) {
                succeed = false;
                ArrUtil.printArray(arr1);
                ArrUtil.printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = ArrUtil.generateRandomArray(maxSize, maxValue);
        ArrUtil.printArray(arr);
        mergeSort(arr);
        ArrUtil.printArray(arr);
    }
}
